package com.wry.netty.client.chatroom;

import com.wry.netty.handler.ClientMsgHandler;
import com.wry.netty.handler.ClientTransferMsgHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * <h1> 聊天客户端 <h1>
 *
 * @author wry
 * @since 2023/4/18 15:57
 */
public class ChatroomClientApp {

    public static void main(String[] args) {
        final NioEventLoopGroup eventExecutors = new NioEventLoopGroup();

        try {
            final Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(eventExecutors)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            socketChannel.pipeline().addLast(new ClientTransferMsgHandler(), new ClientMsgHandler());
                        }
                    }).option(ChannelOption.SO_KEEPALIVE, true);

            final ChannelFuture channelFuture = bootstrap.connect("localhost", 8888).sync();
            channelFuture.channel().closeFuture().sync();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            eventExecutors.shutdownGracefully();
        }
    }
}
